from django.contrib import admin
from django.utils.html import format_html

from imagekit.admin import AdminThumbnail
from import_export.admin import ExportActionModelAdmin

from .models import ServeGift, Store, ConsumeRecord, EnterStoreRecord
from .resources import ServeGiftResource
from .forms import ConsumeRecordAdminForm, ServeGiftAdminForm


@admin.register(Store)
class StoreAdmin(admin.ModelAdmin):
    list_display = ["id", "name"]

    def has_change_permission(self, request, obj=None):
        if request.user.is_superuser:
            return True
        return False

    def has_delete_permission(self, request, obj=None):
        return False


class ConsumeRecordStackedInlineAdmin(admin.StackedInline):
    form = ConsumeRecordAdminForm
    model = ConsumeRecord
    extra = 1
    fieldsets = [
        (None, {'fields': [('recipient', 'department', 'count', 'purpose')]})
    ]

    def has_change_permission(self, request, obj=None):
        if request.user.is_superuser:
            return True
        return False

    def has_delete_permission(self, request, obj=None):
        return False


@admin.register(ServeGift)
class ServeGiftAdmin(ExportActionModelAdmin):
    list_display = ["name", "brand", "specification", "amount", "store_location", "buy_date", "poster_thumbnail"]
    resource_class = ServeGiftResource
    inlines = [ConsumeRecordStackedInlineAdmin]
    date_hierarchy = 'buy_date'
    ordering = ('name', 'brand', 'store', 'buy_date')
    list_filter = ('store',)
    search_fields = ("name", "brand", "specification", "store__name", "buy_date")
    poster_thumbnail = AdminThumbnail(image_field='poster_thumbnail')
    poster_thumbnail.short_description = '封面'
    form = ServeGiftAdminForm

    # def get_queryset(self, request):
    #     qs = super().get_queryset(request)
    #     if not request.user.is_superuser:
    #         user_groups = request.user.groups.values_list("id", flat=True)
    #         qs = qs.filter(store_id__in=user_groups)
    #     return qs

    def get_inlines(self, request, obj):
        if obj:
            return self.inlines
        return []

    def has_delete_permission(self, request, obj=None):
        return False

    def amount(self, obj):
        amount = f"{obj.count}{obj.unit}"
        if obj.count == 0:
            return format_html(f'<span style="color:red">{amount}</span>')
        elif obj.count < 5:
            return format_html(f'<span style="color:orange">{amount}</span>')
        return amount

    amount.short_description = "数量/单位"


@admin.register(ConsumeRecord)
class ConsumeRecordAdmin(ExportActionModelAdmin):
    list_display = ['get_recipient', 'gift', 'amount', 'purpose', 'created']
    search_fields = ("recipient", "department", "purpose", "gift__name", "gift__brand")
    ordering = ('gift', 'count', 'created')
    form = ConsumeRecordAdminForm

    def has_change_permission(self, request, obj=None):
        if request.user.is_superuser:
            return True
        return False

    def has_delete_permission(self, request, obj=None):
        return False


@admin.register(EnterStoreRecord)
class EnterStoreAdmin(ExportActionModelAdmin):
    list_display = ['gift', 'amount', 'store_location', "created"]

    def store_location(self, obj):
        return obj.gift.store_location()
    store_location.short_description = '存放位置'

    def amount(self, obj):
        return f'{obj.count}{obj.gift.unit}'
    amount.short_description = "数量"

    def has_change_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

    def has_add_permission(self, request):
        return False
